package com.example.daily.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.daily.data.dao.ChatMessageDao
import com.example.daily.data.dao.FoodRecordDao
import com.example.daily.data.dao.UserDao
import com.example.daily.data.dao.WeightRecordDao
import com.example.daily.data.entity.ChatMessage
import com.example.daily.data.entity.FoodRecord
import com.example.daily.data.entity.User
import com.example.daily.data.entity.WeightRecord
import com.example.daily.data.util.Converters

@Database(
    entities = [
        User::class,
        WeightRecord::class,
        FoodRecord::class,
        ChatMessage::class
    ],
    version = 5,
    exportSchema = false
)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
    abstract fun weightRecordDao(): WeightRecordDao
    abstract fun foodRecordDao(): FoodRecordDao
    abstract fun chatMessageDao(): ChatMessageDao

    companion object {
        private const val DATABASE_NAME = "daily_food_db"

        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    DATABASE_NAME
                )
                .fallbackToDestructiveMigration()
                .build()
                INSTANCE = instance
                instance
            }
        }
    }
} 